using System.IO;
using System.Reflection;
using Microsoft.Win32;

namespace BLL.Utils
{
    public class AutoStartHelper
    {
        public const string RunRegKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

        public string ProgramName;
        public string ProgramPath;

        public AutoStartHelper()
        {
            ProgramName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location);
            ProgramPath = Assembly.GetExecutingAssembly().Location;
        }

        public bool IsEnabled
        {
            get
            {
                using (var rkApp = Registry.CurrentUser.OpenSubKey(RunRegKey, false))
                    return rkApp != null && rkApp.GetValue(ProgramName) != null;
            }

            set
            {
                using (var rkApp = Registry.CurrentUser.OpenSubKey(RunRegKey, true))
                {
                    if (rkApp != null)
                    {
                        if (value)
                            rkApp.SetValue(ProgramName, ProgramPath);
                        else
                            rkApp.DeleteValue(ProgramName, false);
                    }
                }
            }
        }
    }
}